package com.gamex.battle.client;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.serialization.ClassResolvers;
import io.netty.handler.codec.serialization.ObjectDecoder;
import io.netty.handler.codec.serialization.ObjectEncoder;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import com.gamex.battle.common.Battle;
import com.gamex.battle.common.Constants;


public class BattleClient {

	private final String host;
    private final int port;
    private Battle info;

    public BattleClient(String host, int port, Battle info) {
        this.host = host;
        this.port = port;
        this.info = info;
    }

    public void run() throws Exception {
        Bootstrap b = new Bootstrap();
        try {
            b.group(new NioEventLoopGroup())
             .channel(NioSocketChannel.class)
             .remoteAddress(host, port)
             .handler(new ChannelInitializer<SocketChannel>() {
                @Override
                public void initChannel(SocketChannel ch) throws Exception {
                    ch.pipeline().addLast(
                            new ObjectEncoder(),
                            new ObjectDecoder(ClassResolvers.cacheDisabled(null)),
                            new BattleClientHandler(info));
                }
             });

             b.connect().sync().channel();
            
        } finally {}
    }

    public static void main(String[] args) throws Exception {
        String host = Constants.SERVER_IP;
        int port = Constants.SERVER_PORT;
    	new BattleClient(host, port, new Battle().setService(Battle.SERVICE.LOGIN).setUserId("james1")).run();
    }
}